import 'package:flutter/material.dart';

///重写的Listtile
///官方控件中的ListTile控件存在头部锁死大小等问题
///在此重写
class AppListTile extends StatelessWidget {
  ///头部控件
  final Widget leading;

  ///标题控件
  final Widget title;

  ///副标题控件
  final Widget subTitle;

  ///尾部控件
  final Widget trailing;

  ///内边距
  final EdgeInsetsGeometry padding;

  ///点击事件
  final Function onTap;

  ///长按事件
  final Function onLongPress;

  ///圆角样式
  final BorderRadius radius;

  ///颜色
  final Color color;

  ///垂直对齐方式
  final CrossAxisAlignment crossAxisAlignment;

  const AppListTile({
    Key key,
    this.leading,
    this.title,
    this.subTitle,
    this.trailing,
    this.padding = EdgeInsets.zero,
    this.onTap,
    this.onLongPress,
    this.radius = BorderRadius.zero,
    this.color,
    this.crossAxisAlignment = CrossAxisAlignment.center,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    ///标题内容
    final List<Widget> _titleContent = [];

    ///判断标题是否为空
    ///若标题不为空，则添加到标题内容列中
    if (title != null)
      _titleContent.add(
        DefaultTextStyle(
          style: Theme.of(context).textTheme.bodyText1,
          child: title,
        ),
      );

    ///判断副标题是否为空
    ///若标题不为空，则添加到标题内容列中
    if (subTitle != null)
      _titleContent.add(
        DefaultTextStyle(
          style: Theme.of(context).textTheme.bodyText2.copyWith(fontSize: 12),
          child: subTitle,
        ),
      );

    ///使用FlatButton进行事件的添加与样式的调整
    return FlatButton(
      shape: RoundedRectangleBorder(borderRadius: radius),
      clipBehavior: Clip.antiAlias,
      materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
      onPressed: onTap,
      onLongPress: onLongPress,
      padding: padding,
      color: color,

      ///因为控件整体框架为水平布局，此处使用Row进行处理
      child: Row(
        crossAxisAlignment: crossAxisAlignment,
        children: [
          leading ?? Container(),
          _titleContent == null
              ? Container
              : Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: _titleContent,
                  ),
                ),
          trailing ?? Container(),
        ],
      ),
    );
  }
}
